<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      :title="sysBusinessReportDict.reportDictionaryInformation"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
      @closed="closeDialog"
    >
      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          :title="sysBusinessReportDict.dictionaryCoding"
          :label-width="formLabelWidth"
          prop="dictCode"
          :error="errorMsg.dictCode"
        >
          <el-input v-model="formData.dictCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :title="sysBusinessReportDict.dictionaryName"
          :label-width="formLabelWidth"
          prop="dictName"
          :error="errorMsg.dictName"
        >
          <el-input v-model="formData.dictName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :title="sysBusinessReportDict.explanatoryNote"
          :label-width="formLabelWidth"
          prop="description"
          :error="errorMsg.description"
        >
          <el-input
            v-model="formData.description"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          :title="sysBusinessReportDict.sort"
          :label-width="formLabelWidth"
          prop="sort"
          :error="errorMsg.sort"
        >
          <el-input
            v-model="formData.sort"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit('formData')">{{
          operateType.save
        }}</el-button>
        <el-button @click="cancel">{{ operateType.cancel }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  create,
  update,
} from "../../../api/SysBusiness/SysBusinessReportDict.js";
export default {
  name: "SysBusinessReportDictEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        dictCode: "",
        dictName: "",
        description: "",
        sort: 0,
      },
      formData: {
        id: 0,
        dictCode: "",
        dictName: "",
        description: "",
        sort: 0,
      },
      formDataRules: {
        dictCode: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        dictName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        description: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
      },
      initErrorMsg: {
        dictCode: "",
        dictName: "",
        description: "",
        sort: "",
      },
      errorMsg: {
        dictCode: "",
        dictName: "",
        description: "",
        sort: "",
      },
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessReportDict() {
      return this.$t("SysBusinessReportDict") || {};
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.$refs.formData.resetFields();
      this.$parent.getList();
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      // 动画结束后在清空表单 防止页面闪烁
      // this.$refs.formData.resetFields();
      // this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },

    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("dictCode", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("dictName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("description", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("sort", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
